package com.eascs.admin.sys.permission.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.sys.permission.form.SysResourcesForm;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.entity.sys.SysResources;
import com.eascs.workflow.common.service.sys.SysPermissionService;
import com.eascs.workflow.common.vo.ModelResult;

@RestController
@RequestMapping("/act/resource/rest")
public class ResourceRestController extends BaseRestController {
	@Reference
	private SysPermissionService sysPermissionService;
	@Value("${o2o.admin.ztree.diycss}")
	private String diycss;//图标样式

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public BaseRestResult<String> list() {
		ModelResult<List<SysResources>> modelResult = sysPermissionService.selectResourcesList();
		List<SysResources> list = modelResult.getModel();
		for(int i=0;i<list.size();i++){
			SysResources item = list.get(i);
			if(item.getResourceType()==StatusContexts.RESOURCE_TYPE_QX){
				item.setIconSkin(diycss);
			}
		}
		return BaseRestResult.buildRestResult(JSON.toJSONString(list));
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public BaseRestResult<String> update(SysResourcesForm form) {
		SysResources sysResources = SysResourcesConvert(form);
		List<SysResources> list = sysPermissionService.selectResourcesListByCode(sysResources.getResourceCode());
		int count = list.size();
		if (count == 1 && (!list.get(0).getId().equals(sysResources.getId()))) {
			return BaseRestResult.warn("资源Code不能重复!");
		}
		if (count > 1) {
			return BaseRestResult.warn("资源Code不能重复!");
		}
		ModelResult<Integer> modelResult = sysPermissionService.updateResources(sysResources);
		Integer result = modelResult.getModel();
		if (result > 0) {
			return BaseRestResult.buildSuccessResult();
		} else {
			return BaseRestResult.warn("修改失败!");
		}
	}
	
	@RequestMapping(value = "/updateParentId", method = RequestMethod.POST)
	public BaseRestResult<String> updateParentId(String resourcesId,String parentId) {
		
		SysResources sysResources =  sysPermissionService.getResourcesByIds(resourcesId);
		sysResources.setParentId(parentId);
		User user=UserHolder.getUser();
		sysResources.setUpdateTime(new Date());
		sysResources.setUpdator(user.getNo());
		
		Integer result = sysPermissionService.update(sysResources);
		if (result > 0) {
			return BaseRestResult.buildSuccessResult();
		} else {
			return BaseRestResult.warn("修改失败!");
		}
	}
	

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public BaseRestResult<String> save(SysResourcesForm form) {
		SysResources sysResources = SysResourcesConvert(form);
		List<SysResources> list = sysPermissionService.selectResourcesListByCode(sysResources.getResourceCode());
		int count = list.size();
		if (count >= 1) {
			return BaseRestResult.warn("资源Code不能重复!");
		}
		ModelResult<SysResources> modelResult = sysPermissionService.insertResources(sysResources);
		SysResources result = modelResult.getModel();
		if(result.getResourceType()==StatusContexts.RESOURCE_TYPE_QX){
			result.setIconSkin(diycss);
		}
		if (result.getId() != null) {
			return BaseRestResult.buildRestResult(JSON.toJSONString(result));
		} else {
			return BaseRestResult.warn("新增失败!");
		}
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public BaseRestResult<String> delete(SysResources sysResources) {
		ModelResult<Integer> modelResult = sysPermissionService.deleteResources(sysResources);
		Integer result = modelResult.getModel();
		if (result > 0) {
			return BaseRestResult.buildSuccessResult();
		} else {
			return BaseRestResult.warn("删除失败!");
		}
	}

	private SysResources SysResourcesConvert(SysResourcesForm resform) {
		User user = UserHolder.getUser();
		SysResources sysres = new SysResources();
		sysres.setId(resform.getId());
		sysres.setResourceName(resform.getResourceName());
		sysres.setResourceCode(resform.getResourceCode());
		sysres.setParentId(resform.getParentId());
		sysres.setResourceType(resform.getResourceType());
		sysres.setResourceDefault(resform.getResourceDefault());
		sysres.setResourceUrl(resform.getResourceUrl());
		sysres.setDescription(resform.getDescription());
		sysres.setSort(resform.getSort());
		sysres.setUpdateTime(new Date());
		sysres.setCreateTime(new Date());
		sysres.setUpdator(user.getAccount());
		sysres.setCreator(user.getAccount());
		return sysres;
	}

}
